<route lang="json5" type="page">
{
  style: {
    navigationBarTitleText: '修改套餐',
  },
}
</route>

<template>
  <view style="height: 100%; margin-top: 30rpx">
    <wd-cell title="游戏币数量" required>
      <wd-input-number v-model="gameCount" />
    </wd-cell>

    <wd-cell title="套餐价格" required>
      <wd-input-number v-model="packagePrice" :precision="2" :step="0.1" />
    </wd-cell>

    <wd-button custom-class="m-3" block @click="sumit()">保存</wd-button>
  </view>
</template>

<script lang="ts" setup>
import { onLoad, onReachBottom } from '@dcloudio/uni-app'
import { http, httpGet, httpPost } from '@/utils/http'

const id = ref(0)

// 游戏币数量
const gameCount = ref(0)

const packagePrice = ref(0)

const init = async (id) => {
  const _res = await httpGet(`/api/app/order/package/info?id=${id}`)

  packagePrice.value = _res.packagePrice / 100
  gameCount.value = _res.gameCount
}

// // onload
onLoad(async (options) => {
  if (options.id) {
    try {
      init(options.id)

      id.value = options.id
    } catch (error) {}
  }
})

const sumit = async () => {
  try {
    uni.showLoading({
      title: '提交中',
      mask: true,
    })

    if (id.value) {
      await httpPost('/api/app/order/package/update', {
        id: id.value ? id.value * 1 : undefined,
        gameCount: gameCount.value,
        packagePrice: packagePrice.value * 100,
      })
    } else {
      await httpPost('/api/app/order/package/add', {
        gameCount: gameCount.value,
        packagePrice: packagePrice.value * 100,
      })
    }

    uni.showToast({
      title: '提交成功',
      icon: 'none',
    })

    uni.navigateBack()
  } catch (error) {}
}
</script>

<style lang="scss" scoped></style>
